<template>
    <div class="register-container">
        <div class="register-wrapper">
            <a-card class="register-card">
                <div class="register-header">
                    <div class="register-icon">
                        <icon-user-add class="icon" />
                    </div>
                    <h2 class="register-title">创建账户</h2>
                    <p class="register-subtitle">请填写您的账户信息</p>
                </div>

                <a-form ref="registerFormRef" :model="registerForm" :rules="formRules" class="register-form">
                    <div class="required-hint">
                        <span class="required-star">*</span> 号为必填项
                    </div>
                    <div class="input-section">
                        <div class="input-label">
                            <icon-mobile />
                            <span>手机号 <span class="required-star">*</span></span>
                        </div>
                        <a-form-item field="phone" hide-label>
                            <a-input v-model="registerForm.phone" placeholder="请输入手机号" size="large" :max-length="11"
                                allow-clear />
                        </a-form-item>
                    </div>

                    <div class="input-section">
                        <div class="input-label">
                            <icon-lock />
                            <span>密码 <span class="required-star">*</span></span>
                        </div>
                        <a-form-item field="password" hide-label>
                            <a-input-password v-model="registerForm.password" placeholder="请输入密码" size="large"
                                allow-clear />
                        </a-form-item>
                    </div>

                    <div class="input-section">
                        <div class="input-label">
                            <icon-email />
                            <span>邮箱</span>
                        </div>
                        <a-form-item field="email" hide-label>
                            <a-input v-model="registerForm.email" placeholder="请输入邮箱" size="large" allow-clear />
                        </a-form-item>
                    </div>

                    <div class="input-section">
                        <div class="input-label">
                            <icon-user />
                            <span>昵称</span>
                        </div>
                        <a-form-item field="nickName" hide-label>
                            <a-input v-model="registerForm.nickName" placeholder="请输入昵称" size="large" allow-clear />
                        </a-form-item>
                    </div>

                    <div class="button-section">
                        <a-button type="primary" size="large" :loading="loading" :disabled="loading"
                            @click="handleRegister" class="register-button">
                            <template #icon>
                                <icon-check-circle />
                            </template>
                            完成注册
                        </a-button>
                    </div>
                </a-form>

                <div class="login-section">
                    <p>已有账户？</p>
                    <a-button type="outline" size="medium" @click="goToLogin" class="login-button-secondary">
                        <template #icon>
                            <icon-export />
                        </template>
                        立即登录
                    </a-button>
                </div>
            </a-card>

            <div class="register-decoration">
                <div class="decoration-circle circle-1"></div>
                <div class="decoration-circle circle-2"></div>
                <div class="decoration-circle circle-3"></div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import userAPI from '@src/api/user/user.js'

const router = useRouter()
const registerFormRef = ref()

const registerForm = reactive({
    phone: '',
    email: '',
    password: '',
    nickName: ''
})

const loading = ref(false)

const formRules = {
    phone: [
        { required: true, message: '请输入手机号' },
        {
            validator: (value, callback) => {
                if (!/^1[3-9]\d{9}$/.test(value)) {
                    callback('请输入正确的手机号')
                } else {
                    callback()
                }
            }
        }
    ],
    password: [
        { required: true, message: '请输入密码' },
        { minLength: 6, message: '密码长度至少为6位' }
    ],
    email: [
        {
            validator: (value, callback) => {
                if (value && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
                    callback('请输入正确的邮箱格式')
                } else {
                    callback()
                }
            }
        }
    ],
    nickName: []
}

const handleRegister = async () => {
    // 表单验证
    const validate = await registerFormRef.value.validate()
    if (validate) return

    try {
        loading.value = true

        const response = await userAPI.register({ ...registerForm })
        if (response.code == 200) {
            Message.success('注册成功！')
            router.push('/login')
        } else {
            Message.error(response.message || '注册失败，请重试')
        }
    } catch (err) {
        const message = err?.response?.data?.message ?? err.message ?? '注册失败，请重试'
        Message.error(message)
    } finally {
        loading.value = false
    }
}

const goToLogin = () => {
    router.push('/login')
}
</script>

<style scoped>
.register-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #165dff 0%, #14c9c9 50%, #722ed1 100%);
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    padding: 20px;
    position: relative;
    overflow: hidden;
}

@keyframes gradientBG {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

.register-wrapper {
    position: relative;
    z-index: 2;
    padding: 20px;
    max-width: 100%;
    box-sizing: border-box;
}

.register-card {
    width: 520px;
    max-width: 100%;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 16px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    margin: 0 auto;
}

.register-header {
    text-align: center;
    padding: 32px 32px 24px;
}

.register-icon {
    width: 64px;
    height: 64px;
    background: linear-gradient(135deg, #4f46e5, #7c3aed, #ec4899);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
    box-shadow: 0 8px 20px rgba(79, 70, 229, 0.3);
}

.register-icon .icon {
    font-size: 28px;
    color: white;
}

.register-title {
    font-size: 28px;
    font-weight: 600;
    color: #1d2129;
    margin-bottom: 8px;
    background: linear-gradient(135deg, #165dff, #722ed1);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.register-subtitle {
    font-size: 14px;
    color: #86909c;
    margin: 0;
}

.input-section {
    margin-bottom: 20px;
}

.input-label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
    color: #1d2129;
    font-weight: 500;
    font-size: 14px;
}

.required-star {
    color: #ff4d4f;
    margin-left: 2px;
}

.required-hint {
    margin-bottom: 16px;
    font-size: 12px;
    color: #86909c;
    text-align: left;
}


.button-section {
    margin-top: 24px;
    margin-bottom: 20px;
}

.register-button {
    background: linear-gradient(135deg, #4f46e5, #7c3aed, #ec4899);
    border: none;
    font-weight: 600;
    font-size: 16px;
    height: 48px;
    border-radius: 12px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 15px rgba(79, 70, 229, 0.3);
    position: relative;
    overflow: hidden;
    width: 100%;
}

.register-button::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
}

.register-button:hover {
    transform: translateY(-3px);
    box-shadow: 0 12px 30px rgba(124, 58, 237, 0.4);
}

.register-button:hover::before {
    left: 100%;
}

.register-button:active {
    transform: translateY(-1px);
}

.login-section {
    text-align: center;
    padding: 24px 32px;
    background: rgba(247, 248, 250, 0.8);
    border-top: 1px solid #e5e6eb;
}

.login-section p {
    margin: 0 0 12px 0;
    color: #86909c;
    font-size: 14px;
}

.login-button-secondary {
    background: linear-gradient(135deg, #06b6d4, #3b82f6, #8b5cf6);
    border: none;
    font-weight: 600;
    font-size: 14px;
    height: 40px;
    border-radius: 10px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 12px rgba(6, 182, 212, 0.3);
    color: white;
}

.login-button-secondary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(59, 130, 246, 0.4);
    background: linear-gradient(135deg, #0891b2, #2563eb, #7c3aed);
}

.register-decoration {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

.decoration-circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    animation: float 6s ease-in-out infinite;
}

.circle-1 {
    width: 80px;
    height: 80px;
    top: 20%;
    left: 10%;
    animation-delay: 0s;
}

.circle-2 {
    width: 120px;
    height: 120px;
    top: 60%;
    right: 15%;
    animation-delay: 2s;
}

.circle-3 {
    width: 60px;
    height: 60px;
    bottom: 20%;
    left: 20%;
    animation-delay: 4s;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0px);
    }

    50% {
        transform: translateY(-20px);
    }
}

@media (max-width: 480px) {
    .register-wrapper {
        padding: 10px;
    }

    .register-card {
        width: 100%;
        margin: 0;
        border-radius: 12px;
    }

    .register-header {
        padding: 20px 20px 12px;
    }

    .register-title {
        font-size: 22px;
    }

    .register-form {
        padding: 0 20px;
    }

    .login-section {
        padding: 16px 20px;
    }
}
</style>